package com.leonyr.smartipaddemo.crop;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.leonyr.mvvm.vm.LViewModel;
import com.leonyr.smartipaddemo.adapter.ImageAdapter;
import com.leonyr.smartipaddemo.base.photo.IImageView;
import com.leonyr.smartipaddemo.bean.PicItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Date： 2020/6/15 0015
 * Time： 17:36
 * Created by xtqb.
 */
class CropImageViewHolder<V extends IImageView> extends LViewModel implements ImageAdapter.OnItemClickListener {
    private V view;
    private List<PicItem> list;
    private ImageAdapter adapter;
    private int lastID = -1;

    public CropImageViewHolder(@NonNull Context ctx) {
        super(ctx);
        list = new ArrayList<>();
        adapter = new ImageAdapter(list, this);
    }

    public void reflesh(List<PicItem> bitmaps) {
        if (list.size() > 0) {
            list.clear();
        }
        list.addAll(bitmaps);
        if(list.size()>0){
            list.get(0).setSelected(true);
            view.cropImage(list.get(0).getBitmap());
            lastID = 0;
        }
        adapter.notifyDataSetChanged();
        bitmaps.clear();
    }

    public ImageAdapter getAdapter() {
        return adapter;
    }

    public void attachView(V view) {
        this.view = view;
    }

    public void dispose() {
        view = null;
        if (list != null) {
            list.clear();
            list = null;
        }
        adapter = null;
    }

    public void clean() {
        list.clear();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int pos) {
        Log.e("OnclickItem", "============================================== " + pos);
        if (lastID != pos) {
            for (PicItem item : list) {
                if (item.isSelected()) {
                    item.setSelected(false);
                }
            }
            list.get(pos).setSelected(true);
            adapter.notifyDataSetChanged();

            view.cropImage(list.get(pos).getBitmap());
            lastID = pos;
        }
    }

    public void reset() {
        if (list.size() > lastID) {
            list.remove(lastID);
            adapter.notifyDataSetChanged();
            resetId();
            if(list.size()>0){
                list.get(0).setSelected(true);
                view.cropImage(list.get(0).getBitmap());
                lastID = 0;
            }
        }
    }

    public void resetId() {
        lastID = -1;
    }

//    public void setLastID(int selectedId){
//        lastID = selectedId;
//    }

    public String getCurrentPath() {
        if (list == null || list.size() == 0) return "";
        return list.get(lastID).getPath();
    }

    public int getItemCounter() {
        if (list == null) return 0;
        return list.size();
    }

    public List<String> getPaths() {
        List<String> paths = new ArrayList<>();
        for (PicItem item : list) {
            paths.add(item.getPath());
        }
        return paths;
    }
}
